<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
JLoader::register('linioFunctions',JPATH_COMPONENT.DS.'helpers'.DS.'functions.php');

/**
 * 
 * recuperar el articulo para mostrarlo.
 * @param stdClass[] $item
 * @param String $currency
 * @param array $tags
 */
function producto($item,$currency,&$tags){
	$retval = '<div id="produto" class="produto">';
	$prodDescript = linioFunctions::trat(trim($item->productName)); 
	$priceSpecial = '';
	$shippingFree = '';
	$class='preco';
	$tags[] = explode(" ", linioFunctions::removeacents($item->description.' '.$item->productName));
	
	$precio =  number_format($item->price,2); 
	$descuento = 0.00;
	
	$link = linioFunctions::affiliateLink($item->productId,$prodDescript); //$item->productLink
	if(!trim($item->productImageSmall)) {
		$image = '/images/no-image.jpg';
	} else {
		$image = $item->productImageSmall;
	}
	$retval .= '<a href="'.$link.'" target="_blank">
	<div id="imagem"><img width="90" height="90" src="'.$image.'" alt="'.linioFunctions::miniTit($item->productName,100).'" valign="middle" border="0"></div></a>';
		
	// detectar descuento.
	if($item->originalPrice > $item->price){
		$precio = number_format($item->originalPrice,2);
		$descuento = $item->price;
		$class='precoOferta';
	}
	if($descuento > 0){
		$priceSpecial .= JText::_('DISCOUNT').' <b>'.$currency.number_format($descuento,2).'</b>';
	}
	if($item->shippingFree == 'true'){
		$shippingFree = '<div>'.JText::_('FREE_SHIPPING').'</div>';		
	}
			
	$retval .= '<div class="mercadopago">'.$shippingFree.'</div>
	<div class="prodNome"><a href="'.$link.'" target="_blank" class="prodNome">'.linioFunctions::miniTit($item->productName,70).'</a></div>
	<div class="'.$class.'">'.JText::_('PRICE').':<b>'.$currency.$precio.'</b></div>
	<div class="preco">'.$priceSpecial.'</div>
	<div class="vejamais"><a href="'.$link.'" target="_blank" class="vejamais"><b>'.JText::_('SEE_MORE').'</b></a></div>
	</div>';
	return $retval;
}

/* ***************************************************************************************
 * Mostrar resultados.
 * ************************************************************************************** */
//echo '<h1>'.$this->mensaje.'</h1>';
$tags = array();
$articulos = '';
$brand = JRequest::getVar('brand',"");
$aBrand=explode("_", $brand);
if(isset($this->articulos) && $this->articulos->stdItemsArray){	
	foreach ($this->articulos->stdItemsArray as $item) {
		$articulos.= producto($item,$this->pais->currency,$tags);
	}
}
?>
<form action="index.php" name="adminForm" method="post">
<div id="lista">
<h1>
<?php 
if(isset($this->articulos->path_from_root)){
	$vc_esta = '';
	$d = '';
	foreach ($this->articulos->path_from_root as $categoria) {
		$vc_esta .= '&raquo; <a href="'.linioFunctions::getHref($categoria->category,$categoria->id).'" style="text-decoration: none">'. $categoria->description.'</a>';
		$d.=$categoria->category;
	}
	if(sizeof($aBrand) > 1){
		$vc_esta.='&raquo; <b>('.$aBrand[1].')</b>';		
	}	
} else {
	$d=JText::_('BEST_PRODUCTS');
}
if(isset($vc_esta)){
	echo "<div id=\"vc_esta\"><img src=\"components/".JRequest::getVar('option')."/assets/imagens/seta.png\">&nbsp;".JText::_('YOU_ARE_IN')." ".$vc_esta."</div>";
}
?>
<a href="index.php"><?php echo JText::_('home')?><img src="components/<?php echo JRequest::getVar('option')?>/assets/imagens/home.png" alt="Home" /></a>
</h1>
<div id="submenu">
<?php
if(isset($this->articulos->stdCategories) && count($this->articulos->stdCategories) > 0){
	foreach ($this->articulos->stdCategories as $categoria) {
		echo '<div id="sub_categs"><a href="'.linioFunctions::getHrefFromStd($categoria).'">'.$categoria->description.'</a> ('.$categoria->total.')</div>';		
	}
} 
else if(isset($this->articulos->stdBrands)){
	$iCount = 0;
	foreach ($this->articulos->stdBrands as $brand) {
		if(++$iCount <= 20){
			echo '<div id="sub_categs"><a href="'.linioFunctions::getHrefBrand($brand).'">'.$brand->brand.'</a> ('.$brand->total.')</div>';
		} else {			
			break; // solo las primeras 20 marcas
		}
	}
}
?>
</div>
<div id="ordenar">	
<div id="produtos_find"><?php echo $this->pagination->getResultsCounter(); ?></div>
<?php
if(isset($this->sortCB)){
	echo JText::_('SORT_BY').$this->sortCB;
} 
?>
</div>
<div id="content_produtos"><?php echo $articulos ?></div>
<div class="paginas"><?php echo $this->pagination->getPagesCounter().' - '.JText::_('SHOW').':'.$this->pagination->getLimitBox(JRequest::getVar('limit')).' '.JText::_('ITEMS_PER_PAGE').' '.$this->pagination->getPagesLinks() ?></div><br/>
<input type="hidden" name="option" value="<?php echo JRequest::getVar('option')?>"> 
<input type="hidden" name="category" value="<?php echo JRequest::getVar('category') ?>"> 
<input type="hidden" name="task" value="<?php echo JRequest::getVar('task')?>">
<input type="hidden" name="view" value="<?php echo JRequest::getVar('view')?>"> 
<input type="hidden" name="country" value="<?php echo JRequest::getVar('country') ?>">
<input type="hidden" name="limitstart" value="<?php echo $this->pagination->limitstart?>">
<input type="hidden" name="search" value="<?php echo htmlentities(JRequest::getVar('search'),ENT_QUOTES) ?>">
<input type="hidden" name="brand" value="<?php  echo JRequest::getVar('brand') ?>">
<?php echo JHTML::_( 'form.token' ); // security to prevent XSS as CSRF(Cross Site Request Forgery). ?>
</div>
<!--
JURI::base() = <?php echo JURI::base().PHP_EOL ?>
JURI::base(true) = <?php echo JURI::base(true).PHP_EOL ?>
JURI::current() = <?php echo JURI::current().PHP_EOL ?>
JURI::current(true) = <?php echo JURI::current(true).PHP_EOL ?>
JURI::root()  ) <?php echo JURI::root().PHP_EOL ?>
JURI::root(true)  ) <?php echo JURI::root( true ).PHP_EOL?>
JFactory::getURI()->toString() = <?php echo JFactory::getURI()->toString().PHP_EOL ?>
-->
</form>
<?php 
//validar si se requieren keyword en el header
if(isset($this->includeWord) && isset($this->excludeWord)){	
	$excludeWord = explode(",", $this->excludeWord);
	$includeWord = explode(",", $this->includeWord);
	$tags = linioFunctions::prepareTags($tags);
	$keywords = linioFunctions::createTags($tags,$excludeWord,$includeWord);	
	$this->doc->setMetaData('keywords', $keywords);	
}
// poner metatags
$descripcion = JText::_('DOCUMENT_DESCRIPTION');
$title = $this->titulo.'-'.$d.(isset($aBrand[1])?'-'.$aBrand[1]:'').'-'.$this->pais->name.' '.$this->pagination->getPagesCounter();
$this->doc->setDescription(sprintf($descripcion,$d,JText::_('SITE_NAME')));
$this->doc->setGenerator("Linio Store Component for Joomla! by MAVLSoft(R)");
$this->doc->setTitle($title);
$this->doc->setMetaData('robots','index,no follow');
?>